package com.wang.sort;

import java.util.Arrays;
import java.util.Comparator;

//比较器
public class Demo22_Comparator {
    public static class Student {
        public String name;
        public int id;
        public int age;

        public Student(String name, int id, int age) {
            this.name = name;
            this.id = id;
            this.age = age;
        }
    }
    //按id升序
    public static class IdAscendingComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.id - o2.id;
        }
    }
    //按age升序
    public static class AgeAscendingComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.age - o2.age;
        }
    }
    //按id降序
    public static class IdDescendingComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o2.id - o1.id;
        }
    }
    //按age降序
    public static class AgeDecendingComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o2.age - o1.age;
        }
    }
    //按age升序，age相同时id小的排在前面
    public static class AgeAndIdAscendingComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.age != o2.age ? (o1.age - o2.age) : (o1.id - o2.id);
        }
    }
    public static void printStudents(Student[] students) {
        for (Student student: students
             ) {
            System.out.println("Name :" + student.name + ", Id :" + student.id + ", Age : " + student.age);
        }
    }
    //
    public static void printArray(Integer[] arr) {
        if(arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + "");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Student student1 = new Student("A", 2, 20);
        Student student2 = new Student("C", 1, 21);
        Student student3 = new Student("B", 4, 23);
        Student student4 = new Student("D", 3, 22);

        Student[] students = new Student[] {student1, student2, student3, student4};

        System.out.println("=======================");
        System.out.println("first print");
        Arrays.sort(students, new IdAscendingComparator());
        printStudents(students);

        System.out.println("=======================");
        System.out.println("second print");
        Arrays.sort(students, new IdDescendingComparator());
        printStudents(students);

        System.out.println("=======================");
        System.out.println("third print");
        Arrays.sort(students, new AgeDecendingComparator());
        printStudents(students);

        System.out.println("=======================");
        System.out.println("forth print");
        Arrays.sort(students, new AgeAscendingComparator());
        printStudents(students);

    }
}
